package db;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
public class SqlServer {
	
    public Connection conn = null; // 声明Connection对象的实例
    public Statement stmt = null; // 声明Statement对象的实例
    public ResultSet rs = null; // 声明ResultSet对象的实例
    private static Vector<String> dwVector =new Vector<String>();
    
    //private static MainFrame frame = MainFrame.open();
    
    private FileInputStream inputFile;
    private static SqlServer jdbc;
    private static String dbClassName;//定义保存数据库驱动的变量
    private static String dbUrl;
    private static String dbUser;
    private static String dbPwd;
    
    private static JFrame show;

    /**
     * 返回单位代码
     * @return
     */
    public synchronized static Vector<String> getDwVector(){
    	if(jdbc == null){
    		jdbc = new SqlServer("pubdata.properties");
    	}
    	String sql = "select dwdm from unit";
    	ResultSet result=jdbc.executeQuery(sql);
    	if(dwVector == null){
             dwVector= new Vector<String>();
    	}
    	dwVector.removeAllElements();
    	// dwVector.add("");
        try {
			while(result.next()){
				dwVector.add(result.getString(1));
			}
			return dwVector;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// jdbc.close();
		return dwVector;
    	
	}
    
    /**
     * 返回单位时间
     * @return
     */
    public synchronized static String getDate(String dwdm){
     	if(jdbc == null){
    		jdbc =new SqlServer("pubdata.properties");
    	}
    	String sql = "select dwmc from unit where dwdm = '"+ dwdm +"'";
    	ResultSet result=jdbc.executeQuery(sql);
        try {
			if(result.next()){
				return result.getString(1);//返回单位时间
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// jdbc.close();
		return null;
		
	}
    
    public SqlServer(String propertyFileName) {// 带属性文件名的构造方法
        Properties prop = new Properties();// 属性集合对象
        InputStream is = null;
        try {
        	if(inputFile == null){
        		inputFile = new FileInputStream(propertyFileName);
        	}
        	//javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView(); 
        	//String diskPath=fsv.getHomeDirectory()+"oracletype.properties";
            prop.load(inputFile);// 将属性文件流装载到Properties对象中
            
          //   is.close();// 关闭流
            dbClassName = prop.getProperty("dbClassName");
            dbUrl = prop.getProperty("dbUrl");
            dbUser = prop.getProperty("dbUser");
            dbPwd = prop.getProperty("dbPwd");
        } catch (Exception e) {
        	if(show == null){
        		show = new JFrame();
        	}
            JOptionPane.showConfirmDialog(show, "属性文件打开失败！请查看当前路径下是否有（oracletype.properties属性文件）", "提示", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }
 
    public SqlServer() {// 默认的不带参数的构造函数
        try {
            Class.forName(dbClassName);// 1.注册驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(dbClassName);// 1.注册驱动
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//2.建立与数据库的链接
        } catch (Exception ee) {
        	if(show == null){
        		show = new JFrame();
        	}
           JOptionPane.showConfirmDialog(new JFrame(), "数据库连接失败！检查配置文件参数是否正确", "提示", JOptionPane.ERROR_MESSAGE);
           System.exit(0);
        }
        if (conn == null) {
            System.err
                    .println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
                            + dbClassName
                            + "\r\n链接位置:"
                            + dbUrl
                            + "\r\n用户/密码"
                            + dbUser + "/" + dbPwd);
        }
        return conn;
    }
 
    /*
     * 功能：执行查询语句
     */
    public ResultSet executeQuery(String sql) {
        try { // 捕捉异常
            conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
            Statement  ps= conn.createStatement();
            rs= ps.executeQuery(sql);
        } catch (SQLException ex) {
            System.err.println(ex.getMessage()); // 输出异常信息
        }
        return rs; // 返回结果集对象 5.结果处理
    }
 
    /*
     * 功能:执行更新操作
     */
    public int executeUpdate(String sql) {
        int result = 0; // 定义保存返回值的变量
        try { // 捕捉异常
            conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            result = stmt.executeUpdate(sql); // 执行更新操作
        } catch (SQLException ex) {
            result = 0; // 将保存返回值的变量赋值为0
        }
        return result; // 返回保存返回值的变量
    }
 
    /*
     * 功能:关闭数据库的连接
     */
    public void close() {//6.释放资源
        try { // 捕捉异常
            try {
                if (rs != null) { // 当ResultSet对象的实例rs不为空时
                    rs.close(); // 关闭ResultSet对象
                }
            } finally {
                try {
                    if (stmt != null) { // 当Statement对象的实例stmt不为空时
                        stmt.close(); // 关闭Statement对象
                    }
                } finally {
                    if (conn != null) { // 当Connection对象的实例conn不为空时
                        conn.close(); // 关闭Connection对象
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.err); // 输出异常信息
        }
    }
 
}
